/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package de.hpi.generationmanager.tesma;

import de.hpi.generator.helpers.FileHelper;
import de.hpi.generationmanager.config.tesma.TesmaColumn;
import de.hpi.generationmanager.config.tesma.TesmaFunctionalDep;
import de.hpi.generationmanager.config.tesma.TesmaGenerationModel;
import de.hpi.generationmanager.config.tesma.TesmaTable;


/**
 *
 * @author Michael
 */
public class JSONToTesmaConverter {

    public static void saveModelAsTesmaConfig(TesmaGenerationModel aModel, String aTesmaConfigFileName) throws Exception {        
        String aTesmaString = createTesmaFromModel(aModel);
        FileHelper.recreateFile(aTesmaConfigFileName);
        FileHelper.writeTextToFile(aTesmaString, aTesmaConfigFileName, true);        
    }

    protected static String createTesmaFromModel(TesmaGenerationModel aModel) {
        StringBuilder result = new StringBuilder();
        for (TesmaTable aTesmaTable : aModel.tables) {
            result.append(createTableString(aTesmaTable));
        }
        return result.toString().replace('$', '"');
    }

    protected static String createTableString(TesmaTable aTesmaTable) {
        StringBuilder result = new StringBuilder();
        result.append("table={\n\tname=$").append(aTesmaTable.name).
                append("$\n\trows=$").append(aTesmaTable.rows).append("$\n\t");
        
        for (TesmaColumn aColumn : aTesmaTable.columns) {
            result.append(createColumnString(aColumn));
        }
        for(TesmaFunctionalDep aFuncDep: aTesmaTable.functional_deps){
            result.append(createFunctionalDep(aFuncDep));
        }
        result.append("\n}\n");
        return result.toString();
    }

    protected static String createColumnString(TesmaColumn aTesmaColumn) {
        StringBuilder result = new StringBuilder();
        result.append("column={\n\t\tname=$").append(aTesmaColumn.name).
                append(createColumnProperty("datatype", aTesmaColumn.datatype)).
                append(createColumnProperty("length", aTesmaColumn.length)).
                append(createColumnProperty("unique", aTesmaColumn.unique)).
                append(createColumnProperty("key", aTesmaColumn.key)).
                append(createColumnProperty("key_group", aTesmaColumn.key_group)).
                append(createColumnProperty("foreignkey", aTesmaColumn.foreignkey)).
                append("$\n\t}\n\t");
        return result.toString();
    }
    
    protected static String createColumnProperty(String key, String value) {
        StringBuilder result = new StringBuilder();
        if(value.length() > 0){
            result.append("$\n\t\t").append(key).append("=$").append(value);
        }
        return result.toString();
        
    }
    
    protected static String createFunctionalDep(TesmaFunctionalDep aTesmaFunctionalDep) {
        
        StringBuilder result = new StringBuilder();
        result.append("functional_dep={\n\t\tlhs=$").append(aTesmaFunctionalDep.lhs).
        append("$\n\t\trhs=$").append(aTesmaFunctionalDep.rhs).append("$\n\t}\n\t");
        
        return result.toString();
        
    }
    
}
